package dao.hibernate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.iterators.EntrySetMapIterator;
import org.hibernate.Query;

import util.Util;

import dao.ThuocDAO;
import model.ChuDe;
import model.LoaiThuoc;
import model.Thuoc;

public class ThuocDAOHibernate extends GenericHibernateDAO<Thuoc, Long>
		implements ThuocDAO {
	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findAllThuocByIdLoaiThuoc(long id) {
		List<Thuoc> list = new ArrayList<Thuoc>();
		Query query = getSession().createQuery("SELECT thuoc.idChuDe, thuoc.tenChuDe, thuoc.chuDeCha, thuoc.loaiThuoc FROM Thuoc AS thuoc WHERE thuoc.loaiThuoc.idLoaiThuoc = :pname ORDER BY thuoc.tenChuDe")
		.setLong("pname", id);
		Iterator iter = query.list().iterator();
		Thuoc thuoc = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idThuoc = (Long)row[0];
			String tenThuoc = (String)row[1];
			ChuDe chuDeCha = (ChuDe)row[2];
			LoaiThuoc loaiThuoc = (LoaiThuoc)row[3];
			thuoc = new Thuoc(tenThuoc, chuDeCha, loaiThuoc, null, null, null, null, null, false);
			thuoc.setIdChuDe(idThuoc);
			list.add(thuoc);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByTenLoaiThuoc(String name) {
		return getSession()
				.createQuery(
						"FROM Thuoc AS thuoc WHERE thuoc.loaiThuoc.tenLoaiThuoc LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByTenThuoc(String name) {
		return getSession()
				.createQuery(
						"FROM Thuoc AS thuoc WHERE thuoc.tenChuDe LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByThanhPhan(String name) {
		return getSession()
				.createQuery(
						"FROM Thuoc AS thuoc WHERE thuoc.thanhPhan LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByChiDinh(String name) {
		return getSession()
				.createQuery(
						"FROM Thuoc AS thuoc WHERE thuoc.chiDinh LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByChongChiDinh(String name) {
		return getSession()
				.createQuery(
						"FROM Thuoc AS thuoc WHERE thuoc.chongChiDinh LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByNhaSanXuat(String name) {
		return getSession()
				.createQuery(
						"FROM Thuoc AS thuoc WHERE thuoc.nhaSanXuat LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByQuyCachDongGoi(String name) {
		return getSession()
				.createQuery(
						"FROM Thuoc AS thuoc WHERE thuoc.quyCachDongGoi LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@Override
	public List<Thuoc> findByOneCondition(String criteria, String keyWord) {
		List<Thuoc> list = new ArrayList<Thuoc>();
		if (criteria.equals("tenLoaiThuoc")) {
			list = findByTenLoaiThuoc(keyWord);
		} else if (criteria.equals("tenThuoc")) {
			list = findByTenThuoc(keyWord);
		} else if (criteria.equals("thanhPhan")) {
			list = findByThanhPhan(keyWord);
		} else if (criteria.equals("chiDinh")) {
			list = findByChiDinh(keyWord);
		} else if (criteria.equals("chongChiDinh")) {
			list = findByChongChiDinh(keyWord);
		} else if (criteria.equals("quyCachDongGoi")) {
			list = findByQuyCachDongGoi(keyWord);
		} else if (criteria.equals("nhaSanXuat")) {
			list = findByNhaSanXuat(keyWord);
		}
		
		List<Thuoc> result = new ArrayList<Thuoc>();
		Thuoc thuocSimple = null;
		for (Thuoc thuoc : list) {
			thuocSimple = new Thuoc(thuoc.getTenChuDe(), thuoc.getChuDeCha(), thuoc.getLoaiThuoc(), null, null, null, null, null, false);
			thuocSimple.setIdChuDe(thuoc.getIdChuDe());
			result.add(thuocSimple);
		}
		return result;
	}

	@Override
	public List<Thuoc> findByKeyWord(String keyWord) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Thuoc> findByManyConditions(MultiMap conditions) {
		List<Thuoc> result = null;
		EntrySetMapIterator iterator = new EntrySetMapIterator(conditions);
		String criteria = "";
		List<String> keywords = new ArrayList<String>();
		String keyword = "";
		while (iterator.hasNext()) {
			criteria = (String) iterator.next();
			keywords = (ArrayList<String>) conditions.get(criteria);
			for (String k : keywords) {
				keyword = k;
				if (criteria.equals("tenThuoc")) {
					List<Thuoc> list = findByTenThuoc(keyword);
					result = Util.retainAllThuoc(result, list);
				} else if (criteria.equals("thanhPhan")) {
					List<Thuoc> list = findByThanhPhan(keyword);
					result = Util.retainAllThuoc(result, list);
				} else if (criteria.equals("chiDinh")) {
					List<Thuoc> list = findByChiDinh(keyword);
					result = Util.retainAllThuoc(result, list);
				} else if (criteria.equals("chongChiDinh")) {
					List<Thuoc> list = findByChongChiDinh(keyword);
					result = Util.retainAllThuoc(result, list);
				} else if (criteria.equals("quyCachDongGoi")) {
					List<Thuoc> list = findByQuyCachDongGoi(keyword);
					result = Util.retainAllThuoc(result, list);
				} else if (criteria.equals("nhaSanXuat")) {
					List<Thuoc> list = findByNhaSanXuat(keyword);
					result = Util.retainAllThuoc(result, list);
				}
			}
		}
		return result;
	}

}
